# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  include TagsHelper
  
  def home_path
    '/'
  end
  
  # Draw the menu
  def menu
    home = '<li id="home_nav">' + link_to("Home", root_path) + '</li>'
    manage_users = '<li id="users_nav">' + link_to("Manage Users", manage_users_path) + '</li>'
  	configurations = '<li id="configurations_nav">' + link_to("Configurations", :controller => 'configurations', 
  			:action => 'index') + '</li>'
  	search = '<li id="search_nav">' + link_to("Search", :controller => 'search', :action => 'index') + '</li>'
  	programs = '<li id="program_nav">' + link_to("Programs", :controller => 'programs', :action => 'index') + '</li>'
  	audio_assets = '<li id="audio_assets_nav">' + link_to("Audio Assets", :controller => 'audio_assets', 
  			:action => 'index') + '</li>'
  	authorships = '<li id="authorship_nav">' + link_to("Author's Space", :controller => 'authorships',
        :action => 'index') + '</li>'
    tagging = '<li id="tagging_nav">' + link_to('Tagging', tagging_path) + '</li>'
      
  	menu_anonymous = home + search + audio_assets + programs + tagging
  	# The menu elements with conditions needs to be converted to string when the condition returns false (implies result nil)
  	menu_registered = menu_anonymous + (authorships if logged_in? && current_user.is_author?).to_s
  	
  	menu_editor = menu_registered + manage_users
  	
  	menu_administrator = menu_editor + configurations
	
    if logged_in?
	  if current_user.has_role? Role::Admin
	    menu = '<ul id="navlist">'
	    menu << menu_administrator
	    menu << '</ul>'
	  elsif current_user.has_role? Role::Editor
	    menu = '<ul id="navlist">'
	    menu << menu_editor
	    menu << '</ul>'
	  elsif current_user.has_role? Role::Registered
	    menu = '<ul id="navlist">'
	    menu << menu_registered
	    menu << '</ul>'
	  end
	else
	  menu = '<ul id="navlist">'
	  menu << menu_anonymous
	  menu << '</ul>'
	end
  end
  
  # Returns a message to indicate that are required fields
  def awareness_message
    '<span class="sidebar_title">Be aware that</span>All fields with ' + image_tag('required_bg.gif') + ' symbol are required.'
  end
  
  # Returns true if administration privileges are required
  def administration_required
    unless current_user.is_administrator?
      flash[:error] = "You need administration privileges"
      redirect_back_or_default(login_path)
    end
	end
  
  def tag_search_title(tag)
    "<span class='results_title'>Results for \"#{tag}\" tag</span>"
  end
  
  def related_tags_title
    "<span class='sidebar_title'>Related Tags</span>"
  end
  
  def css_tag_elements
    %w(tag1 tag2 tag3 tag4 tag5 tag6)
  end
end